package com.nciae.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nciae.dto.CartDTO;
import com.nciae.enums.ProductStatusInfoEnum;
import com.nciae.enums.ResultEnum;
import com.nciae.exception.SellException;
import com.nciae.po.ProductInfo;
import com.nciae.dao.ProductInfoMapper;
import com.nciae.service.IProductInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Iterator;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author YangChen
 * @since 2021-10-14
 */
@Service
public class ProductInfoServiceImpl extends ServiceImpl<ProductInfoMapper, ProductInfo> implements IProductInfoService {

    @Override
    @Transactional
    public List<ProductInfo> findAll() {
        return baseMapper.selectList(null);
    }

    @Override
        public IPage<ProductInfo> findByPage(Integer current, Integer size) {
        Page page = new Page();
        page.setCurrent(current);
        page.setSize(size);
        IPage pages = baseMapper.selectPage(page, null);
        return pages;
    }

    @Override
    @Transactional
    public List<ProductInfo> findAllByProductStatus(Integer codeStatus) {
        QueryWrapper<ProductInfo> wrapper = new QueryWrapper<ProductInfo>();
        wrapper.eq("product_status", codeStatus);
        List<ProductInfo> productInfos = baseMapper.selectList(wrapper);
        return productInfos;
    }

    @Override
    @Transactional
    public List<ProductInfo> insert(ProductInfo productInfo) {
        baseMapper.insert(productInfo);
        return findAll();
    }

    @Override
    public List<ProductInfo> update(ProductInfo productInfo) {
        System.out.println(productInfo);
        baseMapper.updateById(productInfo);
        return findAll();
    }

    @Override
    @Transactional
    public ProductInfo findOne(String productId) {
        ProductInfo productInfo = baseMapper.selectById(productId);
        return productInfo;
    }

    @Override
    @Transactional
    public List<ProductInfo> findUpAll() {
        QueryWrapper<ProductInfo> wrapper = new QueryWrapper<ProductInfo>();
        wrapper.eq("product_status",ProductStatusInfoEnum.PUT.getCode());
        List<ProductInfo> productInfos = baseMapper.selectList(wrapper);
        return productInfos;
    }

    @Override
    public  IPage<ProductInfo> findPages(IPage page) {
        IPage pages = baseMapper.selectPage(page, null);
        return pages;
    }

    @Override
    public ProductInfo onSale(String productId) throws SellException {
        ProductInfo productInfo = baseMapper.selectById(productId);
        if (productInfo == null) {
            throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
        }
        if (productInfo.getProductStatus().equals(ProductStatusInfoEnum.PUT.getCode())) {
            throw new SellException(ResultEnum.ORDER_STATUS_ERROR);
        }
        productInfo.setProductStatus(ProductStatusInfoEnum.PUT.getCode());
        update(productInfo);
        return productInfo;
    }

    @Override
    public ProductInfo offSale(String productId) throws SellException {
        ProductInfo productInfo = baseMapper.selectById(productId);
        if (productInfo == null) {
            throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
        }
        if (productInfo.getProductStatus().equals(ProductStatusInfoEnum.OFF.getCode())) {
            throw new SellException(ResultEnum.ORDER_STATUS_ERROR);
        }
        productInfo.setProductStatus(ProductStatusInfoEnum.OFF.getCode());
        update(productInfo);
        return productInfo;
    }

    @Override
    @Transactional
    public void increaseStock(List<CartDTO> cartDTOList) throws SellException {
        Iterator<CartDTO> iterator = cartDTOList.iterator();
        while (iterator.hasNext()) {
            CartDTO cartDTO = iterator.next();
            ProductInfo productInfo = findOne(cartDTO.getProductId());
            if (productInfo == null) {
              throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
            }
            Integer productQuantity = cartDTO.getProductQuantity() + productInfo.getProductStock();
            productInfo.setProductStock(productQuantity);
            update(productInfo);
        }
    }

    @Override
    @Transactional
    public void decreaseStock(List<CartDTO> cartDTOList) throws SellException {

        Iterator<CartDTO> iterator = cartDTOList.iterator();
        while (iterator.hasNext()) {
            CartDTO cartDTO = iterator.next();
            ProductInfo productInfo = findOne(cartDTO.getProductId());
            if (productInfo == null) {
                throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
            }
            Integer productQuantity = productInfo.getProductStock() - cartDTO.getProductQuantity();
            if (productQuantity < 0) {
                throw new SellException(ResultEnum.PRODUCT_STOCK_ERROR);
            }
            productInfo.setProductStock(productQuantity);
            update(productInfo);
        }
    }
}
